package com.usian.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.entity.Ans;
import com.usian.entity.Ques;
import com.usian.entity.QuesVO;
import com.usian.service.AnsService;
import com.usian.service.QuesService;
import com.usian.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 陈宇轩
 * @version 1.0
 * @date 2023/8/30 11:21
 */

@RestController
@RequestMapping("/ques")
public class QuesContorller {
    @Autowired
    private QuesService quesService;
    @Autowired
    private AnsService ansService;
    @GetMapping("findAllqueList")
    public ResultResponse findAllqueList(Ques que){
        try {
            List<QuesVO> list = quesService.findAllqueList(que);
            return ResultResponse.Success(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("findAllqueList2")
    public ResultResponse findAllqueList2(Ques que){
        try {
            List<QuesVO> list = quesService.findAllqueList2(que);
            return ResultResponse.Success(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }
    @GetMapping("findAllgetQue")
    public ResultResponse findAllgetQue(Integer queSort){
        try {
            List<QuesVO> que1 =  quesService.findAllqueList3(queSort);
            if (que1.size()==0){
                return ResultResponse.Errer();
            }
            return ResultResponse.Success(que1);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }

    @GetMapping("findAllprevious")
    public ResultResponse findAllprevious(Integer queSort){
        try {
            Integer sort = queSort-1;
            List<QuesVO> que1 =  quesService.findAllqueList3(sort);
            if (que1.size()==0){
                return ResultResponse.Errer();
            }
            return ResultResponse.Success(que1);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }

    @GetMapping("findAllnextQue")
    public ResultResponse findAllnextQue(Integer queSort){
        try {
            Integer sort = queSort+1;
            List<QuesVO> que1 =  quesService.findAllqueList3(sort);
            if (que1.size()==0){
                return ResultResponse.Errer();
            }
            return ResultResponse.Success(que1);
        }catch (Exception e){
            e.printStackTrace();
            return ResultResponse.Errer();
        }
    }


    @GetMapping("findAllgetzd")
    public ResultResponse findAllgetzd(){
        List<Ques> list = quesService.list();
        int size = list.size();
        return ResultResponse.Success(size);
    }

    @GetMapping("findAllgetyz")
    public ResultResponse findAllgetyz(){
        List<Ans> list = ansService.list();
        int size = list.size();
        return ResultResponse.Success(size);
    }

    @GetMapping("findAllsetAns")
    public ResultResponse findAllsetAns(Integer queId , String ops){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId, queId);
        Ans ans111 = ansService.getOne(wrapper);
        if (ans111==null){
            Ans ans = new Ans();
            ans.setQueAns(ops);
            ans.setQueId(queId);
            ansService.save(ans);
            return ResultResponse.Success("");
        }else {
            Ans ans = new Ans();
            ans.setId(ans111.getId());
            ans.setQueAns(ops);
            ans.setQueId(ans111.getQueId());
            ansService.updateById(ans);
            return null;
        }

    }
    @GetMapping("getxuanze")
    public ResultResponse getxuanze(Integer queId){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId,queId);
        Ans ans = ansService.getOne(wrapper);
        return ResultResponse.Success(ans.getQueAns());

    }


    @GetMapping("getAns")
    public ResultResponse getAns(Integer queId , String ops){
        LambdaQueryWrapper<Ans> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Ans::getQueId, queId);
        Ans ans1 = ansService.getOne(wrapper);
        System.out.println(ans1);
        return ResultResponse.Success(ans1);

    }

}
